<template>
  <div class="app-container">
    <el-tabs :active-name="activeName" v-model="queryParams.messageType" @tab-click="handleMessageType">
      <el-tab-pane label="应用外" name="1"></el-tab-pane>
      <el-tab-pane label="应用内" name="2"></el-tab-pane>
    </el-tabs>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属机构" prop="tenantId">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入所属机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--      <el-form-item label="推送时间" prop="pushTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.pushTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择推送时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item label="推送时间">
        <el-date-picker
          v-model=dateRange
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>


      <el-form-item label="渠道" key="pushChannel" prop="pushChannel">
        <el-select v-model="queryParams.pushChannel" style="width: 100%;">
          <el-option
            v-for="dict in pushChannelList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['business:pushRecord:add']"-->
    <!--        >新增-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['business:pushRecord:edit']"-->
    <!--        >修改-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['business:pushRecord:remove']"-->
    <!--        >删除-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['business:pushRecord:export']"-->
    <!--        >导出-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="pushRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="所属机构" align="center" prop="tenantName"/>
      <el-table-column label="编码" align="center" prop="templateCode"/>
      <el-table-column label="模板名称" align="center" prop="templateName" show-overflow-tooltip />
      <el-table-column label="推送渠道" align="center" key="pushChannel" prop="pushChannel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.push_channel" :value="scope.row.pushChannel"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="messageHeader" show-overflow-tooltip />
      <el-table-column label="触发行为" align="center"
                       prop="triggeringBehavior">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.triggering_behavior" :value="scope.row.triggeringBehavior"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="接收人" align="center" prop="recipient"/>

      <el-table-column label="消息状态" align="center" prop="messageState" v-if="showMessageState &&activeName==='2'">
        <template slot-scope="scope">
          <span>{{ messageStatusMap[scope.row.messageState] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="推送时间" align="center" prop="pushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pushTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['business:pushRecord:edit']"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['business:pushRecord:remove']"-->
      <!--          >删除-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-info"
            @click="handleDetail(scope.row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!--    &lt;!&ndash; 添加或修改推送记录信息对话框 &ndash;&gt;-->
    <!--    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>-->
    <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled=true>-->
    <!--        <el-form-item label="机构" prop="tenantName">-->
    <!--          <el-input v-model="form.tenantName" placeholder="请输入机构名称"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="模板名称" prop="templateName">-->
    <!--          <el-input v-model="form.templateName" placeholder="请输入模板名称"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="模板编码" prop="templateCode">-->
    <!--          <el-input v-model="form.templateCode" placeholder="请输入模板编码"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="推送渠道" key="pushChannel" prop="pushChannel">-->

    <!--          <el-descriptions-item label="推送渠道">-->
    <!--            <dict-tag-->
    <!--              :options="dict.type.push_channel"-->
    <!--              :value="form.pushChannel"-->
    <!--            />-->
    <!--          </el-descriptions-item>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="推送条件编码" prop="pushConditionCode">-->
    <!--          <el-input v-model="form.pushConditionCode" placeholder="请输入推送条件编码"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="推送结果" prop="pushResult">-->
    <!--          <el-input v-model="form.pushResult" type="textarea" placeholder="请输入内容"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="消息标题" prop="messageHeader">-->
    <!--          <el-input v-model="form.messageHeader" placeholder="请输入消息标题"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="消息内容">-->
    <!--          <editor v-model="form.messageContent" :min-height="192"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="触发行为：1.立即推送，2.定时推送，3.重复推送，4.指定条件推送" prop="triggeringBehavior">-->
    <!--          <el-input v-model="form.triggeringBehavior"-->
    <!--                    placeholder="请输入触发行为：1.立即推送，2.定时推送，3.重复推送，4.指定条件推送"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="推送时间" prop="pushTime">-->
    <!--          <el-date-picker clearable-->
    <!--                          v-model="form.pushTime"-->
    <!--                          type="date"-->
    <!--                          value-format="yyyy-MM-dd"-->
    <!--                          placeholder="请选择推送时间">-->
    <!--          </el-date-picker>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="用户id" prop="userId">-->
    <!--          <el-input v-model="form.userId" placeholder="请输入用户id"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="接收人" prop="recipient">-->
    <!--          <el-input v-model="form.recipient" placeholder="请输入接收人"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="业务id" prop="businessId">-->
    <!--          <el-input v-model="form.businessId" placeholder="请输入业务id"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="消息状态：0.已读；1.未读" prop="messageState">-->
    <!--          <el-input v-model="form.messageState" placeholder="请输入消息状态：0.已读；1.未读"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="操作人id" prop="operatorId">-->
    <!--          <el-input v-model="form.operatorId" placeholder="请输入操作人id"/>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="备注" prop="remark">-->
    <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--      <div slot="footer" class="dialog-footer">-->
    <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
    <!--        <el-button @click="cancel">取 消</el-button>-->
    <!--      </div>-->
    <!--    </el-dialog>-->

    <el-dialog
      :title="'详情'"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <div class="content">
        <div class="item">
          <div class="label dot">机构：</div>
          <div class="value">{{ detail.tenantName }}</div>
        </div>
        <div class="item">
          <div class="label dot">模板名称：</div>
          <div class="value">{{ detail.templateName }}</div>
        </div>
        <div class="item">
          <div class="label dot">模板编码：</div>
          <div class="value">{{ detail.templateCode }}</div>
        </div>
        <div class="item">
          <div class="label dot">推送渠道：</div>
          <div class="value">
            <dict-tag :options="dict.type.push_channel" :value="detail.pushChannel"></dict-tag>
          </div>
        </div>
        <div class="item">
          <div class="label dot">标题：</div>
          <div class="value">{{ detail.messageHeader }}
          </div>
        </div>
        <div class="item">
          <div class="label dot">内容：</div>
          <div class="value">{{ detail.messageContent }}
          </div>
        </div>
        <div class="item">
          <div class="label dot">触发行为：</div>
          <div class="value">
            <dict-tag :options="dict.type.triggering_behavior" :value="detail.triggeringBehavior"></dict-tag>
            <div class="value-time-desc" v-if="detail.triggeringBehavior === '2'">
              {{ detail.triggeringTime }}
            </div>
            <div class="value-time-desc" v-if="detail.triggeringBehavior === '4'">
              {{ detail.conditionName }}
            </div>
          </div>
        </div>
        <div class="item">
          <div class="label dot">接收人：</div>
          <div class="value">{{ detail.recipient }}
          </div>
        </div>
        <div class="item">
          <div class="label dot">状态：</div>
          <div class="value">{{ messageStatusMap[detail.messageState] }}
          </div>
        </div>
        <div class="item">
          <div class="label dot">推送时间：</div>
          <div class="value">{{ parseTime(detail.pushTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
      <el-button @click="open = false">关闭</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import {getPushRecord, listPushRecord} from "@/api/messageCenter/pushRecord";

export default {
  name: "PushRecord",
  dicts: ["push_channel", "triggering_behavior"],
  data() {
    return {
      // 字典状态映射
      messageStatusMap: {
        0: "已读",
        1: "未读"
      },

      activeName: "1",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推送记录信息表格数据
      pushRecordList: [],
      detail: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 是否显示消息状态
      showMessageState: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        tenantName: null,
        templateName: null,
        templateCode: null,
        pushChannel: null,
        pushConditionCode: null,
        pushResult: null,
        messageHeader: null,
        messageContent: null,
        triggeringBehavior: null,
        pushTime: null,
        userId: null,
        recipient: null,
        businessId: null,
        messageState: null,
        messageType: '1',
        operatorId: null,
        triggeringTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户id不能为空", trigger: "blur"}
        ],
      }
    };
  },
  computed: {
    // 推送渠道列表
    pushChannelList() {
      if (this.activeName === "1" ) {
        return [{label: "企业微信", value: "1", }, {label: "钉钉", value: "2",}]
      }
      if (this.activeName === "2") {
        return [{label: "商家小程序", value: "3"}, {label: "租客小程序", value: "4"}]
      }
      return []
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询推送记录信息列表 */
    getList() {
      this.loading = true;
      listPushRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.pushRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pushRecordId: null,
        tenantId: null,
        tenantName: null,
        templateName: null,
        templateCode: null,
        pushChannel: null,
        pushConditionCode: null,
        pushResult: null,
        messageHeader: null,
        messageContent: null,
        triggeringBehavior: null,
        pushTime: null,
        userId: null,
        recipient: null,
        businessId: null,
        messageState: null,
        messageType: null,
        createBy: null,
        updateBy: null,
        operatorId: null,
        createTime: null,
        updateTime: null,
        remark: null,
        triggeringTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pushRecordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加推送记录信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pushRecordId = row.pushRecordId || this.ids
      getPushRecord(pushRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改推送记录信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pushRecordId != null) {
            updatePushRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPushRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pushRecordIds = row.pushRecordId || this.ids;
      this.$modal.confirm('是否确认删除此数据项？').then(function () {
        return delPushRecord(pushRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/pushRecord/export', {
        ...this.queryParams
      }, `pushRecord_${new Date().getTime()}.xlsx`)
    },
    handleDetail(row) {
      getPushRecord(row.pushRecordId).then(response => {
        this.detail = response.data;
        this.open = true;
        this.title = "详情";
        this.detail.triggeringBehavior = this.dicts.type.push_channel.find(item => item.value === this.detail.triggeringBehavior).label;
      });
    },
    handleMessageType(teb) {
      this.activeName = teb.name
      this.queryParams.messageType = teb.name
      this.handleQuery()
    },


  }
};
</script>
<style lang="scss" scoped>

.content {
  .item {
    display: flex;
    padding: 10px 0;

    .label {
      width: 100px;
      font-weight: bold;

      &.dot {
        &:before {
          content: "*";
          color: black;

        }
      }
    }

    .value {
      .value-time {
        display: flex;
        align-items: baseline;
      }
    }
  }
}


</style>
